<?php

class Ext_Db_Table_Row extends Zend_Db_Table_Row
{

    public function __construct( array $config = array() )
    {
        // Hack para poder hacer un setTable despues, este bug lo produce el adapter de Oracle.
        $data = $config[ 'data' ];
        unset( $data[ 'ZEND_DB_ROWNUM' ] );
        $config[ 'data' ] = $data;
        parent::__construct( $config );
    }

    /**
     * Transform a column name from the user-specified form
     * to the physical form used in the database.
     * You can override this method in a custom Row class
     * to implement column name mappings, for example inflection.
     *
     * @param string $columnName Column name given.
     * @return string The column name after transformation applied (none by default).
     * @throws Zend_Db_Table_Row_Exception if the $columnName is not a string.
     */
    protected function _transformColumn( $columnName )
    {
    	return $this->getTable()->getAdapter()->foldCase( parent::_transformColumn( $columnName ) );
    }

    /*
     * La idea es reimplementar esto para soportar formatear las fechas.
     */
    public function __get( $columnName )
    {
        
        $columnName = $this->_transformColumn( $columnName );
        $info = $this->_getTable()->info();
        $metadata = $info[ Zend_Db_Table_Abstract::METADATA ];
        /**
         * ADJI!! agrege la arroba porque me tiraba un notice donde el indice no estaba definido 
         * 
         * Pero si lo estaba de hecho me devuelve el resultado 
         * 
         * No te quise tocar mucho, despues fijate
         */
        $type = strtoupper( @$metadata[ $columnName ][ 'DATA_TYPE' ] );
        if ( $type == "DATE" || $type == "DATETIME" || substr( $type, 0, 9 ) == "TIMESTAMP" ) {
            $data = parent::__get( $columnName );
            if ( empty( $data ) ) {
                return $data;
            }
            return new Ext_Db_Date( $data, Ext_Db_Date::ISO_8601 );
        }
        return parent::__get( $columnName );
    }

    /**
     * Esta function fue modificada para poder serializar la tabla cuando se
     * accede al row por un webService
     *
     * @param Ext_Model_WSProxy $table
     * @return boolean
     * @throws Zend_Db_Table_Row_Exception
     */
    public function setTableInternal( Ext_Model_WSProxy $table = null )
    {
        $this->_table = $table;
        $this->_connected = true;
        return true;
    }


    /**
     * unDeletes existing rows. Copied from Zend_Db_Table_Row
     *
     * @return int The number of rows deleted.
     */
    public function unDelete()
    {
        /**
         * A read-only row cannot be deleted.
         */
        if ($this->_readOnly === true) {
            require_once 'Zend/Db/Table/Row/Exception.php';
            throw new Zend_Db_Table_Row_Exception('This row has been marked read-only');
        }

        $where = $this->_getWhereQuery();

        /**
         * Execute the DELETE (this may throw an exception)
         */
        $result = $this->_getTable()->unDelete($where);

        /**
         * Reset all fields to null to indicate that the row is not there
         */
        $this->_data = array_combine(
            array_keys($this->_data),
            array_fill(0, count($this->_data), null)
        );

        return $result;
    }


}